package com.mao.proxy;

import com.mao.proxy.cglibproxy.AliSmsClass;
import com.mao.proxy.cglibproxy.SmsInterceptor;

import net.sf.cglib.proxy.Enhancer;

public class DynamicProxyCglib {

    public static void main(String[] args) {
        AliSmsClass aliSmsClass = new AliSmsClass(); //被代理类
        Enhancer enhancer = new Enhancer(); //生成动态代理工具
        enhancer.setSuperclass(aliSmsClass.getClass()); //设置Super class
        enhancer.setCallback(new SmsInterceptor()); // 设置方法拦截器
        AliSmsClass o = (AliSmsClass) enhancer.create(); //创建动态代理类
        System.out.println(o.getClass()); //com.mao.proxy.cglibproxy.AliSmsClass$$EnhancerByCGLIB$$12a8d09
        o.sendMessage("123456"); //通过代理类调用被代理类方法
    }
}
